﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Jalal.Library.Data;
using BCGC.DAL;
using Jalal.Library.DataStructure;
using System.Data;
using Jalal.Library.Utility;

namespace BCGC.BLL.BusinessObject.UserAccount
{
    public class UserRole
    {
        private int id;
        public int Id
        {
            get { return id; }
        }

        private string roleName;
        public string RoleName
        {
            get { return roleName; }
            set { roleName = value; }
        }

        private int roleLevel;
        public int RoleLevel
        {
            get { return roleLevel; }
            set { roleLevel = value; }
        }

        private bool isDefaultRole;
        public bool IsDefaultRole
        {
            get { return isDefaultRole; }
            set { isDefaultRole = value; }
        }

        public UserRole()
        {
            this.id = 0;
            this.isDefaultRole = false;
            this.roleLevel = 0;
            this.roleName = string.Empty;
        }

        public UserRole(DataRow dataRow)
        {
            if (dataRow != null)
            {
                if (dataRow.Table.Columns.Contains("RoleId"))
                    this.id = (int)CommonUtility.FilterNull(dataRow["RoleId"], typeof(int));
                if (dataRow.Table.Columns.Contains("IsDefaultRole"))
                    this.isDefaultRole = (bool)CommonUtility.FilterNull(dataRow["IsDefaultRole"], typeof(bool));
                if (dataRow.Table.Columns.Contains("RoleLevel"))
                    this.roleLevel = (int)CommonUtility.FilterNull(dataRow["RoleLevel"], typeof(int));
                if (dataRow.Table.Columns.Contains("RoleName"))
                    this.roleName = (string)CommonUtility.FilterNull(dataRow["RoleName"], typeof(string));
            }
            else
            {
                this.id = 0;
                this.isDefaultRole = false;
                this.roleLevel = 0;
                this.roleName = string.Empty;
            }
        }

        private static ReadWriteCustomCollection<UserRole> LoadFromDataTable(DataTable data)
        {
            ReadWriteCustomCollection<UserRole> result = new ReadWriteCustomCollection<UserRole>();

            if (data != null && data.Rows.Count > 0)
            {
                foreach (DataRow row in data.Rows)
                {
                    result.Add(new UserRole(row));
                }
            }
            return result;
        }

        public static int GetDefaultRoleId()
        {
            DataFactory.GetDataMiner().BeginTransaction(true);
            int roleId = UserData.GetDefaultRoleId();
            DataFactory.GetDataMiner().CommitTransaction();
            return roleId;
        }

        public static ReadWriteCustomCollection<UserRole> GetAllRoles()
        {
            DataFactory.GetDataMiner().BeginTransaction(true);
            ReadWriteCustomCollection<UserRole> roles = LoadFromDataTable(UserData.GetAllRoles());
            DataFactory.GetDataMiner().CommitTransaction();
            return roles;
        }
    }
}
